package com.ag.zlw.dao;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.ag.bean.TbDisposalAdvice;
import com.ag.bean.TbLoginRecord;
import com.ag.common.IDUtil;

/**
 * 登陆日志
 * 
 * @author 钟龙伟
 * 
 */

public class TbLoginRecordDAO extends com.ag.dao.TbLoginRecordDAO {

	private static final Log log = LogFactory.getLog(TbLoginRecordDAO.class);
	private SessionFactory sessionFacotry;

	Session session;
	List<TbLoginRecord> list1;
	
	@Resource
	public void setSessionFacotry(SessionFactory sessionFacotry) {
		this.sessionFacotry = sessionFacotry;
	}

	@PostConstruct
	public void injectSessionFactory() {
		super.setSessionFactory(sessionFacotry);
	}

	public List find(String hql) {
		log.debug("finding from hql");
		try {
			return getHibernateTemplate().find(hql);
		} catch (RuntimeException re) {
			log.error("find by hql", re);
			throw re;
		}
	}

	public List query(final String hsql, final int firstRow, final int maxRow)throws Exception {
        return getHibernateTemplate().executeFind(new HibernateCallback() {
	      public Object doInHibernate(Session s) throws HibernateException,SQLException {
		     Query query = s.createQuery(hsql);
		     query.setFirstResult(firstRow);
		     query.setMaxResults(maxRow);
		     List list = query.list();
		     return list;
		     }
	      });
        }
	
	public List findByTime(final String hsql)throws Exception {
        return getHibernateTemplate().executeFind(new HibernateCallback() {
	      public Object doInHibernate(Session s) throws HibernateException,SQLException {
		     Query query = s.createQuery(hsql);		     		  
		     List list = query.list();
		     return list;
		     }
	      });
        }

	public void delete(List<TbLoginRecord> records) {
		for (TbLoginRecord LoginRecord : records) {
			LoginRecord.setA(IDUtil.DELETE);      // 删除只是把该条记录的"A"字段置为"DELETE"
			super.attachDirty(LoginRecord);
		}
	}

}
